<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 头部按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <tree-table
        class="treeTable"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        :show-row-hover="false"
        border
      >
        <!-- 是否有效列 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted===false"
            style="color:lightgreen;font-size:18px;vertical-align: middle;"
          ></i>
          <i class="el-icon-error" v-else style="color:red;font-size:18px;vertical-align: middle;"></i>
        </template>
        <!-- 排序列 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level==0">一级</el-tag>
          <el-tag size="mini" type="success" v-if="scope.row.cat_level==1">二级</el-tag>
          <el-tag size="mini" type="warning" v-if="scope.row.cat_level==2">三级</el-tag>
        </template>
        <!-- 操作列 -->
        <template slot="operate" slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editCate(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="deleteCate(scope.row)"
          >删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 点击添加分类的对话框 -->
    <el-dialog title="添加分类" :visible.sync="dialogVisible_addCate" width="50%" @close="addCateClose">
      <!-- 中间主体内容 -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <!-- 级联选择框 -->
          <el-cascader
            v-model="arr"
            :options="parentList"
            :props="optionsAdd"
            clearable
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <!-- 底部按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible_addCate = false">取 消</el-button>
        <el-button type="primary" @click="sureAdd">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 点击编辑分类的对话框 -->
    <el-dialog
      title="修改分类"
      :visible.sync="dialogVisible_editCate"
      width="50%"
      @close="editCateClose"
    >
      <!-- 中间主体内容 -->
      <el-form
        :model="editCateForm"
        :rules="editCateFormRules"
        ref="editCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="editCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible_editCate = false">取 消</el-button>
        <el-button type="primary" @click="sureEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //前两集父级分类列表
      parentList: [],
      //级联选择配置对象
      optionsAdd: {
        checkStrictly: true,
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
      },
      // 级联选中的ID数组
      arr: [],
      //添加对话框的隐藏与否
      dialogVisible_addCate: false,
      // 商品分类列表
      cateList: [],
      //表格的列定义
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok",
        },
        {
          label: "排序",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "operate",
        },
      ],
      //定义查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 总数据条数
      total: 0,
      //添加分类表单数据
      addCateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      //添加分类表单数据验证规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
      //编辑数据id

      //修改弹窗
      dialogVisible_editCate: false,
      // 编辑表单数据
      editCateForm: {
        cat_id: 0,
        cat_name: "",
      },
      //编辑规则
      editCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.axios.get("categories", {
        params: this.queryInfo,
      });
      this.total = res.data.total;
      this.cateList = res.data.result;
    },
    //分页大小发生改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    //页码发生改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCateList();
    },
    //点击添加分类按钮
    addCate() {
      this.getParentList();
      this.dialogVisible_addCate = true;
    },
    //获取父级分类列表
    async getParentList() {
      const { data: res } = await this.axios.get("categories", {
        params: { type: 2 },
      });
      if (res.meta.status !== 200) {
        return layer.msg(res.meta.msg, { time: 1000 });
      }
      this.parentList = res.data;
    },
    //添加分类对话框关闭
    addCateClose() {
      this.$refs.addCateFormRef.resetFields();
      this.arr = [];
      this.addCateForm.cat_pid = 0;
      this.addCateForm.cat_level = 0;
    },
    //级联选择发生改变触发
    handleChange() {
      if (this.arr.length > 0) {
        this.addCateForm.cat_pid = this.arr[this.arr.length - 1];
        this.addCateForm.cat_level = this.arr.length;
        return;
      } else {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    },
    //确定添加分类
    sureAdd() {
      this.$refs.addCateFormRef.validate(async (value) => {
        if (!value) {
          return layer.msg("分类名称输入有误！", { time: 1000 });
        }
        const { data: res } = await this.axios.post(
          "categories",
          this.addCateForm
        );
        layer.msg("添加成功！", { time: 1000 }, () => {
          this.getCateList();
          this.dialogVisible_addCate = false;
        });
      });
    },
    // 点击编辑按钮
    editCate(info) {
      this.editCateForm.cat_id = info.cat_id;
      this.editCateForm.cat_name = info.cat_name;
      this.dialogVisible_editCate = true;
    },
    // 点击删除
    deleteCate(info) {
      layer.confirm(
        "此操作将永久删除该分类, 是否继续?",
        { icon: 3, title: "提示" },
        async (index) => {
          const { data: res } = await this.axios.delete(
            "categories/" + info.cat_id
          );
          if (res.meta.status != 200) {
            return layer.msg(res.meta.msg, { time: 1000 });
          }
          layer.msg("删除成功！", { time: 1000 }, () => {
            this.getCateList();
            layer.close(index);
            const a = (this.total % this.queryInfo.pagesize) - 1;
            if (a == 0) {
              this.queryInfo.pagenum--;
              this.getCateList();
            }
          });
        }
      );
    },
    //关闭编辑对话框
    editCateClose() {
      this.$refs.editCateFormRef.resetFields();
    },
    //确定修改
    async sureEdit() {
      const { data: res } = await this.axios.put(
        "categories/" + this.editCateForm.cat_id,
        { cat_name: this.editCateForm.cat_name }
      );
      if (res.meta.status !== 200) {
        return layer.msg(res.meta.msg, { time: 1000 });
      }
      layer.msg("修改成功！", { time: 1000 }, () => {
        this.getCateList();
        this.dialogVisible_editCate = false;
      });
    },
  },
};
</script>

<style>
.treeTable {
  margin: 15px 0;
}
.el-scrollbar__view {
  height: 250px;
}
</style>